<!doctype html>
<html lang="zh-CN"><head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=1313&amp;path=livereload" data-no-instant defer></script>
    <title>薛尧的博客</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="" />

    
    
    
    <link rel="stylesheet" href="../../../css/theme.min.css">

    
    
    
    
    <link rel="stylesheet" href="../../../css/custom.min.css">
    

    
</head>
<body>
        <div id="content" class="mx-auto"><header class="container mt-sm-5 mt-4 mb-4 mt-xs-1">
    <div class="row">
        
        <div class="col-sm-4 col-12 text-sm-right text-center pt-sm-4">
            <a href="../../../" class="text-decoration-none">
                <img id="home-image" class="rounded-circle"
                    
                        src="https://ueyao.github.io/image-hosting/blog/avatar.png"
                    
                />
            </a>
        </div>
        <div class="col-sm-8 col-12 text-sm-left text-center">
        
            <h2 class="m-0 mb-2 mt-4">
                <a href="../../../" class="text-decoration-none">
                    
                        薛尧
                    
                </a>
            </h2>
            <p class="text-muted mb-1">
                
                    Java Developer | Short Video Creator
                
            </p>
            <ul id="nav-links" class="list-inline mb-2">
                
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../about/" title="关于">关于</a>
                    </li>
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../post/" title="文章">文章</a>
                    </li>
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../categories/" title="分类">分类</a>
                    </li>
                
            </ul>
            <ul id="nav-social" class="list-inline">
                
                    <li class="list-inline-item mr-3">
                        <a href="http://github.com/flowstone" target="_blank">
                            <i class="fab fa-github fa-1x text-muted"></i>
                        </a>
                    </li>
                
                    <li class="list-inline-item mr-3">
                        <a href="mailto:xueyao.me#gmail.com" target="_blank">
                            <i class="fas fa-at fa-1x text-muted"></i>
                        </a>
                    </li>
                
            </ul>
        </div>
    </div>
    <hr />
</header>
<div class="container">
    <div class="pl-sm-2">
        <div class="mb-3">
            <h3 class="mb-0">AngularJS动态特效之验证码按钮倒计时</h3>
            
            <small class="text-muted">发布于 2017-11-11</small>
        </div>

        <article>
            <h2 id="功能需求">功能需求</h2>
<p>当用户注册或者找回密码时，输入注册的手机号发送验证码到手机中，点击发送验证码按钮倒计时这个功能是如何实现呢？</p>
<h2 id="效果如下">效果如下</h2>
<p><img src="https://ueyao.github.io/image-hosting/blog/2017/11/2017-11-11_204515.png" alt=""></p>
<h2 id="表单代码如下">表单代码如下</h2>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-html" data-lang="html"><span style="display:flex;"><span>&lt;<span style="color:#f92672">div</span> <span style="color:#a6e22e">class</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;signup&#34;</span> <span style="color:#a6e22e">ng-app</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;signupApp&#34;</span> <span style="color:#a6e22e">ng-controller</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;signupCtrl&#34;</span>&gt;
</span></span><span style="display:flex;"><span>    &lt;<span style="color:#f92672">div</span> <span style="color:#a6e22e">class</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;col-md-9 signupbox&#34;</span>&gt;
</span></span><span style="display:flex;"><span>        &lt;<span style="color:#f92672">form</span> <span style="color:#a6e22e">id</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;signupForm&#34;</span> <span style="color:#a6e22e">action</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;customer_regist.action&#34;</span> <span style="color:#a6e22e">method</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;post&#34;</span> <span style="color:#a6e22e">class</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;form col-md-6&#34;</span>&gt;
</span></span><span style="display:flex;"><span>            &lt;<span style="color:#f92672">div</span> <span style="color:#a6e22e">class</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;form-group&#34;</span>&gt;
</span></span><span style="display:flex;"><span>                &lt;<span style="color:#f92672">label</span> <span style="color:#a6e22e">for</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;inputaccount&#34;</span> <span style="color:#a6e22e">class</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;col-sm-3 control-label&#34;</span>&gt;
</span></span><span style="display:flex;"><span>                    &lt;<span style="color:#f92672">b</span>&gt;*&lt;/<span style="color:#f92672">b</span>&gt;验证码&lt;/<span style="color:#f92672">label</span>&gt;
</span></span><span style="display:flex;"><span>                &lt;<span style="color:#f92672">div</span> <span style="color:#a6e22e">class</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;col-sm-5&#34;</span>&gt;
</span></span><span style="display:flex;"><span>                    &lt;<span style="color:#f92672">input</span> <span style="color:#a6e22e">type</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;text&#34;</span> <span style="color:#a6e22e">name</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;checkcode&#34;</span> <span style="color:#a6e22e">class</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;form-control&#34;</span> <span style="color:#a6e22e">id</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;inputaccount&#34;</span> <span style="color:#a6e22e">placeholder</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;请输入验证码&#34;</span>&gt;
</span></span><span style="display:flex;"><span>                &lt;/<span style="color:#f92672">div</span>&gt;
</span></span><span style="display:flex;"><span>                &lt;<span style="color:#f92672">div</span> <span style="color:#a6e22e">class</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;col-sm-3 song&#34;</span>&gt;
</span></span><span style="display:flex;"><span>                    &lt;<span style="color:#f92672">button</span> <span style="color:#a6e22e">type</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;button&#34;</span> <span style="color:#a6e22e">id</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;checkCode&#34;</span> <span style="color:#a6e22e">class</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;btn btn-default&#34;</span> <span style="color:#a6e22e">ng-bind</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;checkcodemsg&#34;</span> <span style="color:#a6e22e">ng-click</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;getCheckCode()&#34;</span>&gt;获取验证码&lt;/<span style="color:#f92672">button</span>&gt;
</span></span><span style="display:flex;"><span>                &lt;/<span style="color:#f92672">div</span>&gt;
</span></span><span style="display:flex;"><span>            &lt;/<span style="color:#f92672">div</span>&gt;
</span></span><span style="display:flex;"><span>        &lt;<span style="color:#f92672">form</span>&gt;
</span></span><span style="display:flex;"><span>    &lt;/<span style="color:#f92672">div</span>&gt;
</span></span><span style="display:flex;"><span>&lt;/<span style="color:#f92672">div</span>&gt;
</span></span></code></pre></div><h2 id="js代码如下">JS代码如下</h2>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#75715e">&lt;!--</span><span style="color:#a6e22e">验证码倒计时</span><span style="color:#f92672">--&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;</span><span style="color:#a6e22e">script</span> <span style="color:#a6e22e">type</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;text/javascript&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">angular</span>.<span style="color:#a6e22e">module</span>(<span style="color:#e6db74">&#34;signupApp&#34;</span>, [])
</span></span><span style="display:flex;"><span>        .<span style="color:#a6e22e">controller</span>(<span style="color:#e6db74">&#34;signupCtrl&#34;</span>, [<span style="color:#e6db74">&#34;$scope&#34;</span>, <span style="color:#66d9ef">function</span> (<span style="color:#a6e22e">$scope</span>) {
</span></span><span style="display:flex;"><span>            <span style="color:#75715e">//按钮初始化的名字
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>            <span style="color:#a6e22e">$scope</span>.<span style="color:#a6e22e">checkcodemsg</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;获取验证码&#34;</span>;
</span></span><span style="display:flex;"><span>            <span style="color:#75715e">//倒计时变量，默认60秒
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>            <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">second</span> <span style="color:#f92672">=</span> <span style="color:#ae81ff">5</span>;
</span></span><span style="display:flex;"><span>            <span style="color:#75715e">//定时器对象
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>            <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">secondInterval</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">undefined</span>;
</span></span><span style="display:flex;"><span>            <span style="color:#75715e">//是否允许发送验证码的标识 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>            <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">enableFlag</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">true</span>;
</span></span><span style="display:flex;"><span>            <span style="color:#75715e">//获取验证码点击事件
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>            <span style="color:#a6e22e">$scope</span>.<span style="color:#a6e22e">getCheckCode</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">function</span> () {
</span></span><span style="display:flex;"><span>                <span style="color:#75715e">//允许发送验证码
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>                <span style="color:#66d9ef">if</span> (<span style="color:#a6e22e">enableFlag</span>) {
</span></span><span style="display:flex;"><span>                        <span style="color:#75715e">//发送验证码的标识为false，在重新发送时间里不允许再发送
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>                        <span style="color:#a6e22e">enableFlag</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">false</span>;
</span></span><span style="display:flex;"><span>                        <span style="color:#75715e">//禁止按钮
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>                        <span style="color:#a6e22e">$</span>(<span style="color:#e6db74">&#34;#checkCode&#34;</span>).<span style="color:#a6e22e">attr</span>(<span style="color:#e6db74">&#34;disabled&#34;</span>,<span style="color:#e6db74">&#34;true&#34;</span>);
</span></span><span style="display:flex;"><span>                        <span style="color:#75715e">//开启定时器
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>                            <span style="color:#a6e22e">secondInterval</span> <span style="color:#f92672">=</span> window.<span style="color:#a6e22e">setInterval</span>(<span style="color:#66d9ef">function</span> () {
</span></span><span style="display:flex;"><span>                            <span style="color:#75715e">//如果时间为0秒时
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>                            <span style="color:#66d9ef">if</span> (<span style="color:#a6e22e">second</span> <span style="color:#f92672">===</span> <span style="color:#ae81ff">0</span>) {
</span></span><span style="display:flex;"><span>                                <span style="color:#75715e">//重新初始化按钮
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>                                <span style="color:#a6e22e">$scope</span>.<span style="color:#a6e22e">checkcodemsg</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;获取验证码&#34;</span>;
</span></span><span style="display:flex;"><span>                                <span style="color:#75715e">//强制更新视图
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>                                <span style="color:#a6e22e">$scope</span>.<span style="color:#a6e22e">$digest</span>();
</span></span><span style="display:flex;"><span>                                <span style="color:#75715e">//清除定时器
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>                                window.<span style="color:#a6e22e">clearInterval</span>(<span style="color:#a6e22e">secondInterval</span>);
</span></span><span style="display:flex;"><span>                                <span style="color:#75715e">//清空变量
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>                                <span style="color:#a6e22e">secondInterval</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">undefined</span>;
</span></span><span style="display:flex;"><span>                                <span style="color:#75715e">//重置秒数
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>                                <span style="color:#a6e22e">second</span> <span style="color:#f92672">=</span> <span style="color:#ae81ff">5</span>;
</span></span><span style="display:flex;"><span>                                <span style="color:#75715e">//重置验证码标识 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>                                <span style="color:#a6e22e">enableFlag</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">true</span>;
</span></span><span style="display:flex;"><span>                                <span style="color:#75715e">//按钮可用
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>                                <span style="color:#a6e22e">$</span>(<span style="color:#e6db74">&#34;#checkCode&#34;</span>).<span style="color:#a6e22e">removeAttr</span>(<span style="color:#e6db74">&#34;disabled&#34;</span>);
</span></span><span style="display:flex;"><span>                            } <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>                                <span style="color:#75715e">//发送验证码
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>                                <span style="color:#a6e22e">$scope</span>.<span style="color:#a6e22e">checkcodemsg</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;重新发送(&#34;</span><span style="color:#f92672">+</span><span style="color:#a6e22e">second</span> <span style="color:#f92672">+</span> <span style="color:#e6db74">&#39;)秒&#39;</span>;
</span></span><span style="display:flex;"><span>                                <span style="color:#75715e">//强制更新视图
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>                                <span style="color:#a6e22e">$scope</span>.<span style="color:#a6e22e">$digest</span>();
</span></span><span style="display:flex;"><span>                                <span style="color:#75715e">//秒数倒计时
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>                                <span style="color:#a6e22e">second</span><span style="color:#f92672">--</span>;
</span></span><span style="display:flex;"><span>                            }
</span></span><span style="display:flex;"><span>                        }, <span style="color:#ae81ff">1000</span>);
</span></span><span style="display:flex;"><span>                } 
</span></span><span style="display:flex;"><span>            };
</span></span><span style="display:flex;"><span>        }]);
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;</span><span style="color:#960050;background-color:#1e0010">/script&gt;</span>
</span></span></code></pre></div>
        </article>
    </div>

    <div id="disqus_thread"></div>
<script>
    window.disqus_config = function () {
    
    
    
    };
    (function() {
        if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
            document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
            return;
        }
        var d = document, s = d.createElement('script'); s.async = true;
        s.src = '//' + "xie-yao-de-bo-ke" + '.disqus.com/embed.js';
        s.setAttribute('data-timestamp', +new Date());
        (d.head || d.body).appendChild(s);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>

            </div>
        </div><footer class="text-center pb-1">
    <small class="text-muted">
        
        &copy; Copyright 2024
        
        |
        <a href="https://beian.miit.gov.cn/" target="_blank">苏ICP备14012079号</a>
        <br />
        由 <a href="https://gohugo.io/" target="_blank">Hugo</a> 强力驱动
        | 主题 <a href="https://github.com/austingebauer/devise" target="_blank">Devise</a>
        <br />

    </small>
</footer></body>
</html>
